/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.datepicker.client.CalendarUtil;

import java.util.Date;

public class DateUtils {

    private static final long MS_PER_SEC = 1000;
//    private static final long MS_PER_MIN = MS_PER_SEC * 60;
//    private static final long MS_PER_HOUR = MS_PER_MIN * 60;
//    private static final long MS_PER_DAY = MS_PER_HOUR * 24;

    public static Date addYears(Date date, int years) {
        Date result = CalendarUtil.copyDate(date);
        CalendarUtil.addMonthsToDate(result, 12 * years);
        return result;
    }

    @SuppressWarnings("deprecation")
    public static Date getFirstDayOfYear(Date date) {
        Date result = CalendarUtil.copyDate(date);
        result.setDate(1);
        result.setMonth(0);
        result.setHours(0);
        result.setMinutes(0);
        result.setSeconds(0);
        return result;
    }

    @SuppressWarnings("deprecation")
    public static int getMonth(Date date) {
        return date.getMonth();
    }

    private static DateTimeFormat format;

    public static DateTimeFormat getFormat() {
        if (format == null) {
            format = DateTimeFormat.getFormat(Utils.dateFormat);
        }
        return format;
    }

    public static String dateToString(Date date) {
        return date == null ? null : getFormat().format(date);
    }

    public static Date stringToDate(String str) {
        return str == null ? null : getFormat().parse(str);
    }

    public static Date stringToDateTime(String str) {
        return str == null ? null : create(Long.parseLong(str));
    }

    private static Date create(long secs) {
        Date date = new Date();
        date.setTime(secs);
        return date;
    }

    @SuppressWarnings("deprecation")
    public static int getYear(Date value) {
        return 1900 + value.getYear();
    }

    /**
     * Resets the date to have no time modifiers.
     *
     * @param date the date
     */
    @SuppressWarnings("deprecation") // GWT requires Date
    public static void resetTime(Date date) {
        long msec = date.getTime();
        msec = (msec / MS_PER_SEC) * MS_PER_SEC;
        date.setTime(msec);

        // Daylight savings time occurs at midnight in some time zones, so we reset
        // the time to noon instead.
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
    }

    @SuppressWarnings("deprecation")
    public static int getMonthDay(Date value) {
        return value.getDate();
    }
}
